package com.lyz.myself.controller;

import com.lyz.myself.config.utils.Result;
import com.lyz.myself.config.utils.base.CodeMsg;
import com.lyz.myself.config.utils.oss.AliyunOSSUtil;
import com.lyz.myself.config.utils.oss.ConstantProperties;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: myself
 * @Date: 2019/2/21 13:44
 * @Author: Mr.Deng
 * @Description:
 */
@Api(description="通用文件上传接口")
@RestController
@RequestMapping("/fileUploadPublic")
public class FileUploadPublicController {
    private static final Logger log = LoggerFactory.getLogger(FileUploadController.class);
    /**
     * 通用文件上传接口
     * @param files
     */
    @RequestMapping(value = "/fileUpload",method = RequestMethod.POST)
    public Result uploadFile(@Param("files") MultipartFile[] files){
        System.out.println(files.length);
        try {
            if(files.length>0){
                List<Map<String, Object>> fileIdUrl = new ArrayList();
                List<String> fileList = new ArrayList<String>();
                List<Integer> fileSize = new ArrayList();
                List<String> fileName = new ArrayList();
                for(MultipartFile file :files ){
                    String filename = file.getOriginalFilename();
                    log.info("上传"+filename);
                    if(!"".equals(filename.trim())){
                        //指定路径，解决linux没有写入权限
                        File newFile = new File(ConstantProperties.PROP_UPLOAD_FOLDER+filename);
                        if (!newFile.getParentFile().exists()) {
                            newFile.getParentFile().mkdirs();// 新建文件夹
                        }
//                        FileOutputStream os = new FileOutputStream(newFile);
//                        os.write(file.getBytes());
//                        os.close();
                        // 转存文件
                        file.transferTo(newFile);
                        //上传到OSS
                        String[] url = AliyunOSSUtil.uploadObject2OSS(newFile);
                        fileList.add( AliyunOSSUtil.getUrl(ConstantProperties.OSS_BUCKET_NAME,url[1]));
                        Long size = Long.valueOf(file.getSize());
                        fileSize.add(size.intValue());
                        fileName.add(filename);
                        newFile.delete();
                    }
                }
                for (int j = 0; j < fileList.size(); j++) {
                    Map<String, Object> newMap = new HashMap<>();
                    newMap.put("file_path", fileList.get(j));
                    newMap.put("file_name", fileName.get(j));
                    newMap.put("size", fileSize.get(j));
                    fileIdUrl.add(newMap);
                }
                return Result.success(fileIdUrl);
            }
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.error(CodeMsg.ERROR,e.getMessage());
        }
        return Result.error(CodeMsg.ERROR,"文件为空");
    }


}
